package aircraftWar;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Graphics;
import java.util.Arrays;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

/**
 * 说明：世界
 *
 * @author: PYS
 * @date: 2022/11/8
 **/
public class World extends JPanel {
    public static final int WIDTH = 400;
    public static final int HEIGHT = 700;
    public static final int START = 0;
    public static final int RUNNING = 1;
    public static final int PAUSE = 2;
    public static final int GAME_OVER = 3;

    private int state = START;
    private Sky sky = new Sky();
    private Hero hero = new Hero();
    private FlyingObject[] flyingObject = {};
    private Bullet[] bullets = {};

    public FlyingObject nextOne() {
        Random rand = new Random();
        int type = rand.nextInt(20);
        if (type < 5) {
            return new Bee();
        } else if (type < 13) {
            return new Airplane();
        } else {
            return new BigAirplane();
        }
    }

    private int enterIndex = 0;
    public void enterAction() {
        enterIndex++;
        if (enterIndex % 40 == 0) {
            FlyingObject obj = nextOne();
            flyingObject = Arrays.copyOf(flyingObject, flyingObject.length+1);
            flyingObject[flyingObject.length-1] = obj;
        }
    }

    private int shootIndex = 0;
    public void shootAction() {
        shootIndex++;
        if (shootIndex % 5 == 0) {  //5是子弹发射频率
            Bullet[] bs = hero.shoot();
            if (bs.length == 2) {  //当是双倍子弹时
                bullets = Arrays.copyOf(bullets, bullets.length+2);
            } else {
                bullets = Arrays.copyOf(bullets, bullets.length+1);
            }
            System.arraycopy(bs,0, bullets,bullets.length-bs.length, bs.length);
        }
    }

    public void stepAction() {
        sky.step();
        for (int i=0; i<flyingObject.length; i++) {
            flyingObject[i].step();
        }
        for (int i=0; i<bullets.length; i++) {
            bullets[i].step();
        }
    }

    public void isOutOfBoundsAction() {
        for (int i=0; i<flyingObject.length; i++) {
            if (flyingObject[i].isOutOfBounds() || flyingObject[i].isRemove()) {
                flyingObject[i] = flyingObject[flyingObject.length-1];
                flyingObject = Arrays.copyOf(flyingObject, flyingObject.length-1);
            }
        }
        for (int i=0; i<bullets.length; i++) {
            if (bullets[i].isOutOfBounds() || bullets[i].isRemove()) {
                bullets[i] = bullets[bullets.length-1];
                bullets = Arrays.copyOf(bullets, bullets.length-1);
            }
        }
    }

    private int score = 0;
    public void bulletBangAction() {
        for (int i=0; i<bullets.length; i++) {
            Bullet b = bullets[i];
            for (int j=0; j<flyingObject.length; j++) {
                FlyingObject f = flyingObject[j];
                if (b.isLive() && f.isLive() && f.isHit(b)) {
                    f.goDead();
                    b.goDead();
                    if (f instanceof EnemyScore) {
                        EnemyScore es = (EnemyScore)f;
                        score += es.getScore();
                    }
                    if (f instanceof EnemyAward) {
                        EnemyAward ea = (EnemyAward)f;
                        int type = ea.getAwardType();
                        switch (type) {
                            case EnemyAward.FIRE:
                                hero.addFire();
                                break;
                            case EnemyAward.LIFE:
                                hero.addLife();
                                break;
                        }
                    }
                }
            }
        }
    }

    public void heroBangAction() {
        for (int i=0; i<flyingObject.length; i++) {
            FlyingObject f = flyingObject[i];
            if (f.isLive() && hero.isLive() && hero.isHit(f)) {
                f.goDead();
                hero.subtractLife();
                hero.clearFire();
            }
        }
    }

    public void checkGameOverAction() {
        if (hero.getLife() <= 0) {
            state = GAME_OVER;
        }
    }

    public void action() {
        MouseAdapter m = new MouseAdapter() {
            @Override
            public void mouseMoved(MouseEvent e) {
                if (state == RUNNING) {
                    int x = e.getX();
                    int y = e.getY();
                    hero.moveTo(x, y);
                }
            }

            public void mouseClicked(MouseEvent e) {
                switch (state) {
                    case START:
                        state = RUNNING;
                        break;
                    case GAME_OVER:
                        score = 0;
                        sky = new Sky();
                        hero = new Hero();
                        flyingObject = new FlyingObject[0];
                        bullets = new Bullet[0];
                        state = START;
                        break;
                }
            }

            public void mouseExited(MouseEvent e){
                if (state == RUNNING) {
                    state = PAUSE;
                }
            }

            public void mouseEntered(MouseEvent e){
                if (state == PAUSE) {
                    state = RUNNING;
                }
            }
        };
        this.addMouseListener(m);
        this.addMouseMotionListener(m);

        Timer timer = new Timer();
        int interval = 10;
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                if (state == RUNNING) {
                    enterAction();
                    shootAction();
                    stepAction();
                    isOutOfBoundsAction();
                    bulletBangAction();
                    heroBangAction();
                    checkGameOverAction();
                    repaint();
                }
            }
        }, interval, interval);
    }

    public void paint(Graphics g) {
        g.drawImage(sky.getImage(), sky.x, sky.y, null);
        g.drawImage(sky.getImage(), sky.x, sky.getY1(), null);
        g.drawImage(hero.getImage(), hero.x, hero.y, null);
        for (int i=0; i<flyingObject.length; i++) {
            FlyingObject f = flyingObject[i];
            g.drawImage(f.getImage(), f.x, f.y, null);
        }
        for (int i=0; i<bullets.length; i++) {
            Bullet b = bullets[i];
            g.drawImage(b.getImage(), b.x, b.y, null);
        }

        int x = hero.x;
        int y = hero.y;
        int fire = hero.getFire();
        g.drawString("分数: " + score, x, y-20); //画分
        g.drawString("命数: " + hero.getLife(), x+50, y-20); //画命
        g.drawString("火力值: " + hero.getFire(), x+100, y-20); //画命

        switch (state) {
            case START:
                g.drawImage(Images.start, 0, 0, null);
                break;
            case PAUSE:
                g.drawImage(Images.pause, 0, 0, null);
                break;
            case GAME_OVER:
                g.drawImage(Images.gameover, 0, 0, null);
                break;
        }
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        World world = new World();
        world.setFocusable(true);
        frame.add(world);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(WIDTH+16, HEIGHT+39);
        frame.setLocationRelativeTo(null);
        frame.setResizable(false);
        frame.setVisible(true);
        world.action();
    }
}
